<template>
  <div class="app-container">
    <t-form
        v-show="showSearch"
        ref="queryRef"
        :model="queryParams"
        class="mb20"
        layout="inline"
    >
      <t-form-item label="类型名称" prop="name">
        <t-input
            v-model="queryParams.name"
            clearable
            placeholder="请输入类型名称"
            style="width: 200px"
            @enter="handleQuery"
        />
      </t-form-item>
      <t-form-item>
        <t-space>
          <t-button theme="primary" @click="handleQuery">
            搜索
            <template #icon>
              <t-icon name="search"/>
            </template>
          </t-button>
          <t-button theme="default" @click="resetQuery">
            重置
            <template #icon>
              <t-icon name="refresh"/>
            </template>
          </t-button>
        </t-space>
      </t-form-item>
    </t-form>

    <t-row :gutter="10" class="mb8">
      <t-col :span="1.5">
        <t-button
            theme="primary"
            @click="handleAdd"
        >新增
          <template #icon>
            <t-icon name="add"/>
          </template>
        </t-button>
      </t-col>
      <right-toolbar
          v-model:showSearch="showSearch"
          @queryTable="getList"
      ></right-toolbar>
    </t-row>

    <t-table
        v-loading="loading"
        :columns="columns"
        :data="list"
        row-key="id"
        stripe
    >
      <template #action="scope">
        <t-button
            theme="primary"
            variant="text"
            @click="handleUpdate(scope.row)"
        >修改
          <template #icon>
            <t-icon name="edit"/>
          </template>
        </t-button>
        <t-button
            theme="primary"
            variant="text"
            @click="handleDelete(scope.row)"
        >删除
          <template #icon>
            <t-icon name="delete"/>
          </template>
        </t-button>
      </template>
    </t-table>

    <t-dialog v-model:visible="open" attach="body" width="780px">
      <template #header>
        <h3 style="margin: auto">{{ title }}</h3>
      </template>
      <t-form ref="typeRef" :data="form" :rules="rules" label-width="140px">
        <t-form-item label="类型名称" prop="name" requiredMark>
          <t-input
              v-model="form.name"
              placeholder="请输入类型名称"
          />
        </t-form-item>
        <t-form-item label="排序" prop="sort">
          <t-input
              v-model="form.sort"
              placeholder="请输入排序"
              type="number"
          />
        </t-form-item>
      </t-form>
      <template #footer>
        <div class="dialog-footer">
          <t-button theme="primary" @click="submitForm">确 定</t-button>
          <t-button theme="default" @click="cancel">取 消</t-button>
        </div>
      </template>
    </t-dialog>
  </div>
</template>

<script setup>


import {addType, getType, listType, updateType} from "@/api/nbot/actType";
import {getCurrentInstance, reactive, ref, toRefs} from "vue";

const {proxy} = getCurrentInstance();


const list = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const title = ref("");

const columns = ref([
  {colKey: "name", title: "类型名称", width: 100,},
  {colKey: "sort", title: "排序", width: 100, },
  {colKey: "action", title: "操作", width: 100, },
]);

const data = reactive({
  form: {},
  queryParams: {
    name: undefined,
  },
  rules: {
    name: [
      {required: true, message: "类型名称不能为空", trigger: "blur"},
    ]
  },
});

const {queryParams, form, rules} = toRefs(data);

/** 查询列表 */
function getList() {
  loading.value = true;
  listType(queryParams.value).then((response) => {
    list.value = response.data;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    name: undefined,
    sort: 9,
  };
  proxy.resetForm("typeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加活动类型";
}

/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value;
  getType(id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改活动类型";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["typeRef"].validate().then((valid) => {
    if (valid) {
      if (form.value.id !== undefined) {
        updateType(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addType(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const id = row.id || ids.value;
  proxy.$modal
      .confirm('是否确认删除编号为"' + id + '"的数据项？')
      .then(function () {
        return delCategory(id);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      })
      .catch(() => {
      });
}

getList();
</script>
